(function(){
	$.extend($.fn,{
		categoryComboBox: function() {
			var $that_ = $(this);
			var options_ = {
				deep: 1,
				type: 0,
				value: 0,
				isParentValue: false,
				isLoadParent: true,
				isShowNull: true,
				nullName: false,
				onLoad: function($select){},
				onSelect: function(deep, category){}
			};
			var cacheData_ = {};
			var loadRootsPath_ = g_domain + "/category/listTypeRoots/json";
			var loadSonsPath_ = g_domain + "/category/listSons/json";
			var loadBrothersPath_ = g_domain + "/category/listBrothers/json";
			var containerId_ = "selectCat_" + $that_.attr("id") + "_";	
			var loadBrothers_ = function(catId, isPrepend){
				vars = {
					"catId":catId
				};
				$.getJSON(loadBrothersPath_ + "?r=" + Math.random(), vars, function(data){
					makeSelectHtml_(data, catId, isPrepend);
				});
			};
			var loadSons_ = function(catId, value){
				var loadPath = loadRootsPath_;
				var vars = {
					type:options_.type
				};
				if (catId) {
					loadPath = loadSonsPath_;
					vars = {
						"catId":catId
					};
				}
				$.getJSON(loadPath + "?r=" + Math.random(), vars, function(data){
					makeSelectHtml_(data, value);
				});
			};
			var makeSelectHtml_  = function(data, value, isPrepend) {
				if (data.status != 0) {
					alert(data.message);
					return;
				}
				var parent = data.result;
				var list = parent.categories;
				if (!list || list.length == 0) {
					return;
				}
				var deep = parseInt(list[0].deep);
				var selectTagId = containerId_ + deep;
				var $select = $("#" + selectTagId);
				if ($select.length == 0) {
					if (isPrepend) {
						$that_.prepend('<select id="' + selectTagId + '"></select>');
					} else {
						$that_.append('<select id="' + selectTagId + '"></select>');
					}
					$select = $("#" + selectTagId);
					
				} else {
					$select.html("");
				}
				
				if (options_.isShowNull) {
					var nullName = options_.nullName?options_.nullName:"-类目" + deep + "-";
					$select.append("<option value=''>" + nullName + "</option>");
				}
				for (var i = 0; i < list.length; i++) {
					cacheData_[list[i].catId] = list[i];
					$select.append("<option value='" + list[i].catId + "'>" + list[i].name + "</option>");
				}
				
				// 当前列表有选中值,但下一级没有加载,则加载下一级
				if (value && $("#" + containerId_ + (deep + 1)).length == 0) {
					loadSons_(value);
				}
				
				if (options_.isLoadParent && parent && parent.deep >= options_.deep && $("#" + containerId_ + (deep - 1)).length == 0) {
					loadBrothers_(parent.catId, true);
				}
				// 选中值
				$select.val(value);
				options_.onSelect(deep, cacheData_[value]);
				options_.onLoad($select);
			};
			
			if (arguments.length > 0) {
				var options = arguments[0];
				for (var key in options) {
					if (options_[key] != undefined) {
						options_[key] = options[key];
					}
				}
			}
			
			$(document).off("change", "[id^='" + containerId_ + "']");
			$(document).on("change", "[id^='" + containerId_ + "']", function() {
				var value = $(this).val();
				var idSplits = $(this).attr("id").split("_");
				var deep = parseInt(idSplits[idSplits.length -1]);
				$("[id^='" + containerId_ + "']").each(function(i, el) {
					idSplits = $(this).attr("id").split("_");
					var deep2 = parseInt(idSplits[idSplits.length -1]);
					if (deep2 > deep) {
						options_.onSelect(deep2);
						$(this).remove();
					}
				});
				
				options_.onSelect(deep, cacheData_[value]);
				
				if (value) {
					loadSons_(value);
				}
			});
			
			if (options_.value) {
				if (options_.isParentValue) {
					loadSons_(options_.value);
				} else {
					loadBrothers_(options_.value);
				}
			} else {
				loadSons_();
			}
		}
	});
})();